//
//  OrderListViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2019/3/6.
//  Copyright © 2019年 刘喆. All rights reserved.
//

#import "OrderListViewController.h"
#import "OrderListTableViewCell.h"
#import "OrderListModel.h"
#import "AllOrdersViewController.h"
#import "OrderDetailVC.h"

@interface OrderListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSArray *modelsArr;

@end

@implementation OrderListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self loadLayout];
    
    NSDictionary *param = @{@"siteId":@"32"};
    [MLAHttpManage postHttpType:MLAHttpRequestType_Post hostType:MLAHttpHostType_Api api:@"/api/plugins/ss.shopping/ApiOrdersGet" params:param.mj_JSONString success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        NSArray *list = response[@"orderInfoList"];
        _modelsArr = [OrderListModel mj_objectArrayWithKeyValuesArray:list];
        [self.tableView reloadData];
    } failure:^{
        
    }];
}
- (void)loadLayout {
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0.001;
        _tableView.estimatedSectionHeaderHeight = 0.001;
        [_tableView registerClass:[OrderListTableViewCell class] forCellReuseIdentifier:@"orderlist"];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
    }
    return _tableView;
}

#pragma mark -
#pragma mark tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _modelsArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"orderlist" forIndexPath:indexPath];
   
    OrderListModel *model = _modelsArr[indexPath.section];
    cell.numL.text = [NSString stringWithFormat:@"订单编号：%@", model.guid];
    cell.moneyL.text = [NSString stringWithFormat:@"金额：%@", model.totalFee];
    cell.addressL.text = [NSString stringWithFormat:@"地址：%@", model.address];
    cell.dataL.text = [NSString stringWithFormat:@"日期：%@", model.addDate];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

#pragma mark tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 1)];
    footView.backgroundColor = kRGB(245, 245, 245, 1);
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return .0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderListModel *model = _modelsArr[indexPath.section];

    OrderDetailVC *aoVC = [[OrderDetailVC alloc] init];
    aoVC.guid = model.guid;
    [self.navigationController pushViewController:aoVC animated:YES];
}



@end
